package com.xjh.t.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.t.common.Constast;
import com.xjh.t.common.DataGridView;
import com.xjh.t.common.ResultObj;
import com.xjh.t.common.WebUtils;
import com.xjh.t.domain.*;
import com.xjh.t.service.*;
import com.xjh.t.vo.ExpressInfoVo;
import com.xjh.t.vo.GroupAuditInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-05-21
 */
@RestController
@RequestMapping("/expressInfo")
public class ExpressInfoController {

    @Autowired
    private IExpressInfoService expressInfoService;

    @Autowired
    private IGroupInfoService groupInfoService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IAreaInfoService areaInfoService;

    @Autowired
    private IUserService userService;

    @RequestMapping("/loadAllExpress")
    public DataGridView loadAllExpress(ExpressInfoVo expressInfoVo) {
        IPage<ExpressInfo> page = new Page<>(expressInfoVo.getPage(), expressInfoVo.getLimit());
        QueryWrapper<ExpressInfo> queryWrapper = new QueryWrapper<>();

        queryWrapper.like(StringUtils.isNotBlank(expressInfoVo.getEsZoneName()), "es_zone_name", expressInfoVo.getEsZoneName());
        queryWrapper.eq(expressInfoVo.getEsStatus() != null && expressInfoVo.getEsStatus() < 3, "es_status",expressInfoVo.getEsStatus());

        if(StringUtils.isNotBlank(expressInfoVo.getGpName()))
        {
            QueryWrapper<GroupInfo> groupInfoQueryWrapper = new QueryWrapper<>();
            groupInfoQueryWrapper.like("gp_name",expressInfoVo.getGpName());
            List<GroupInfo> groupInfoList = this.groupInfoService.list(groupInfoQueryWrapper);
            List<Integer> gpIds = new ArrayList<>();
            for(GroupInfo groupInfo:groupInfoList)
                gpIds.add(groupInfo.getGpId().intValue());

            queryWrapper.in(gpIds.size() > 0,"gp_id",gpIds);
        }
        //queryWrapper.and(Wrapper->Wrapper.like("es_zone", "3202").or().like("es_zone", "3306"));
        User user =  (User) WebUtils.getSession().getAttribute("user");

        //queryWrapper.and(new QueryWrapper<ExpressInfo>());
        String areas = "";
        List<String> alldutyAreaId = new ArrayList<>();
        Integer index = 0;
        QueryWrapper<GroupInfo> queryWrapperZone = new QueryWrapper<>();



        queryWrapper.orderByDesc("es_id");
        this.expressInfoService.page(page,queryWrapper);
        List<ExpressInfoVo> expressInfoVoList = new ArrayList<>();
        for(ExpressInfo expressInfo:page.getRecords())
        {
            ExpressInfoVo expressInfoVo1 = new ExpressInfoVo();
            BeanUtils.copyProperties(expressInfo,expressInfoVo1);
            GroupInfo groupInfo = this.groupInfoService.getById(expressInfoVo1.getGpId());
            if(groupInfo != null)
            {
                expressInfoVo1.setGpName(groupInfo.getGpName());
                UserInfo userInfo = this.userInfoService.getById(groupInfo.getUrId());
                if(userInfo != null)
                    expressInfoVo1.setCreateUserAccount(userInfo.getUrPhonenumber());

                String addr = "";
                AreaInfo areaInfo = this.areaInfoService.getById(groupInfo.getGpZone());
                while (areaInfo != null && areaInfo.getAaLevel() != Constast.POLICY_TOP_LEVEL)
                {
                    addr = areaInfo.getAaAreaname() + " " + addr;
                    areaInfo = this.areaInfoService.getById(areaInfo.getAaParentid());
                }
                if(areaInfo == null)
                {
                    System.out.println(groupInfo.getGpId() + "," + groupInfo.getGpName());
                }
                else
                    addr = areaInfo.getAaAreaname() + " " + addr;

                if(!expressInfo.getEsAuditor().equals(0))
                {
                    User user1 = this.userService.getById(expressInfo.getEsAuditor());
                    if(user1 != null)
                    {
                        expressInfoVo1.setAuditName(user1.getName());
                    }
                }

                expressInfoVo1.setGpAddr(addr);
            }

            expressInfoVoList.add(expressInfoVo1);

        }

        return new DataGridView(page.getTotal(),expressInfoVoList);

    }

    @RequestMapping("/passOrForbidExpress")
    public ResultObj passOrForbidExpress(ExpressInfoVo expressInfoVo) {

        ExpressInfo expressInfo = this.expressInfoService.getById(expressInfoVo.getEsId());

        User user =  (User) WebUtils.getSession().getAttribute("user");
        if(user == null)
        {
            return  ResultObj.OFFLINE_ERROR;
        }
        if(expressInfo != null && expressInfo.getEsStatus() != expressInfoVo.getEsStatus()) {

            expressInfo.setEsStatus(expressInfoVo.getEsStatus());
            expressInfo.setEsAuditor(user.getId());
            this.expressInfoService.updateById(expressInfo);

            return ResultObj.UPDATE_SUCESS;

        }

        else
            return ResultObj.UPDATE_ADDRESS_ERROR;

    }
}
